package com.shupian.controller;

import com.shupian.domain.ResponseResult;
import com.shupian.domain.dto.LoginUserDTO;
import com.shupian.domain.entity.User;
import com.shupian.enums.AppHttpCodeEnum;
import com.shupian.exception.SystemException;
import com.shupian.service.BlogLoginService;
import com.shupian.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "blog登录相关接口")
@RestController
public class BlogLoginController {
    @Autowired
    private BlogLoginService blogLoginService;
    @ApiOperation(value ="登录接口")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginUserDTO loginUserDTO){
        User user = BeanCopyUtils.copyBean(loginUserDTO, User.class);
        if(!StringUtils.hasLength(user.getUserName())){
            //提示 必须要传用户名
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return blogLoginService.login(user);
    }

    @ApiOperation(value = "登出接口")
    @PostMapping("/logout")
    public ResponseResult logout(){
        return blogLoginService.logout();
    }
}
